package com.ruoyi.qsng.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qsng.domain.KCourse;
import com.ruoyi.qsng.domain.KCoursePurchase;
import com.ruoyi.qsng.domain.TRefund;
import com.ruoyi.qsng.mapper.KCourseMapper;
import com.ruoyi.qsng.mapper.KCoursePurchaseMapper;
import com.ruoyi.qsng.mapper.TRefundMapper;
import com.ruoyi.qsng.service.ITRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 退款记录Service业务层处理
 * 
 * @author gk
 * @date 2023-07-18
 */
@Service
public class TRefundServiceImpl implements ITRefundService
{
    @Autowired
    private TRefundMapper tRefundMapper;

    @Autowired
    private KCourseMapper kCourseMapper;

    @Autowired
    private KCoursePurchaseMapper kCoursePurchaseMapper;

    /**
     * 查询退款记录
     * 
     * @param id 退款记录主键
     * @return 退款记录
     */
    @Override
    public TRefund selectTRefundById(Long id)
    {
        return tRefundMapper.selectTRefundById(id);
    }

    @Override
    public TRefund selectTRefundByOrderId(String orderId )
    {
        return tRefundMapper.selectTRefundByOrderId(orderId);
    }

    /**
     * 查询退款记录列表
     * 
     * @param tRefund 退款记录
     * @return 退款记录
     */
    @Override
    public List<TRefund> selectTRefundList(TRefund tRefund)
    {
        return tRefundMapper.selectTRefundList(tRefund);
    }

    /**
     * 新增退款记录
     * 
     * @param tRefund 退款记录
     * @return 结果
     */
    @Override
    public int insertTRefund(TRefund tRefund)
    {
        tRefund.setCreateTime(DateUtils.getNowDate());
        return tRefundMapper.insertTRefund(tRefund);
    }

    /**
     * 修改退款记录
     * 
     * @param tRefund 退款记录
     * @return 结果
     */
    @Override
    public int updateTRefund(TRefund tRefund)
    {
        //判断是否同意退款
        if(tRefund.getState()==1){
            //更新订单状态
            KCoursePurchase kCoursePurchase =kCoursePurchaseMapper.selectKCoursePurchaseById(tRefund.getOrderId());
            Assert.notNull(kCoursePurchase, "获取订单信息失败！");
            kCoursePurchase.setState(2);
            int i=kCoursePurchaseMapper.updateKCoursePurchase(kCoursePurchase);
            if(i <1){
                Assert.notNull(null, "更新订单信息失败！");
            }

            //返还已购买的名额
            KCourse kCourse= kCourseMapper.selectKCourseById(kCoursePurchase.getCourseId());
            Assert.notNull(kCourse, "获取课程信息失败！");
            kCourse.setSold(kCourse.getSold() -1);
             int j=kCourseMapper.updateKCourse(kCourse);
             if(j < 1){
                 Assert.notNull(null, "更新课程信息失败！");
             }

        }
        tRefund.setUpdateTime(DateUtils.getNowDate());
        return tRefundMapper.updateTRefund(tRefund);
    }

    /**
     * 批量删除退款记录
     * 
     * @param ids 需要删除的退款记录主键
     * @return 结果
     */
    @Override
    public int deleteTRefundByIds(Long[] ids)
    {
        return tRefundMapper.deleteTRefundByIds(ids);
    }

    /**
     * 删除退款记录信息
     * 
     * @param id 退款记录主键
     * @return 结果
     */
    @Override
    public int deleteTRefundById(Long id)
    {
        return tRefundMapper.deleteTRefundById(id);
    }
}
